import { message } from 'antd';
import react from 'react';
import {
  getUserInfo,
  getOrgRootTree,
  getChildOrgListByParentId,
  getPageInfo,
  selectAppByUser,
  selectRoleCodeByUser,
  selectResourceListByRole,
} from '@/services/public';

export default {
  namespace: 'common',
  state: {
    userInfo: {},
    pageInfo: {},
    appAuthList: null,
    appCodeOfSelect: null,
    menuData: [],
  },
  effects: {
    *getUserInfo(_, { put, call, select }) {
      const res = yield call(getUserInfo);
      const { code, data } = res;
      if (code === 0 && data) {
        yield put({
          type: 'updateModel',
          payload: {
            userInfo: data,
          },
        });
        // 获取用户有权限的租户信息
        const res1 = yield call(selectAppByUser, { userId: data.userid });
        const { code: code1, data: data1 } = res1;
        if (code1 === 0 && data1) {
          yield put({
            type: 'updateModel',
            payload: {
              appAuthList: data1,
              appCodeOfSelect: data1[0]?.appCode,
            },
          });
          yield put({
            type: 'getMenuData',
            payload: { userId: data.userid, appCode: data1[0]?.appCode },
          });
        }
      }
    },
    *getMenuData({ payload }, { put, call }) {
      const res = yield call(selectRoleCodeByUser, payload);
      if (res.code === 0) {
        const res1 = yield call(selectResourceListByRole, {
          roleIdList: res.data.data,
          appCode: payload.appCode,
        });
        const { code, data } = res1;
        if (code === 0) {
          const arr = data.resourceList || [];
          let userMenuData = [];
          // 获取用户组件菜单，通过resourceCode: "user_component_menu"筛选子菜单
          const parentId = arr.find((a) => a.resourceCode === 'user_component_menu')?.resourceId;
          userMenuData = userMenuData.concat(
            arr
              .filter((a) => a.parentId === parentId)
              .map((a) => ({
                key: a.path,
                resourceId: a.resourceId,
                text: a.resourceName,
                icon: a.iconClassName, // <span className="iconfont icon-zuzhiguanli" />,
                children: [],
              })),
          );
          userMenuData.forEach((menu) => {
            menu.children = arr
              .filter((a) => a.parentId === menu.resourceId)
              .map((a) => ({
                key: a.path,
                text: a.resourceName,
                icon: a.iconClassName, // <span className="iconfont icon-zuzhiguanli" />,
                children: [],
              }));
          });
          yield put({
            type: 'updateModel',
            payload: {
              menuData: userMenuData,
            },
          });
        }
      }
    },
    *getPageInfo(_, { put, call }) {
      const res = yield call(getPageInfo);
      const { code, data } = res;
      if (code === 0 && data) {
        yield put({
          type: 'updateModel',
          payload: {
            pageInfo: data,
          },
        });
      }
    },
    *getOrgRootTree(_, { put, call, select }) {
      const res = yield call(getOrgRootTree);
      const { code, data } = res;
      if (code === 0 && Array.isArray(data)) {
        return data.map(({ id, orgName, hasChild, ...rest }) => ({
          value: id,
          title: orgName,
          isLeaf: !hasChild,
          ...rest,
        }));
      }
      message.error('获取根组织失败');
      return [];
    },
    *getChildOrgListByParentId({ payload }, { put, call, select }) {
      const res = yield call(getChildOrgListByParentId, payload);
      const { code, data } = res;
      if (code === 0 && Array.isArray(data)) {
        return data.map(({ id, orgName, hasChild, ...rest }) => ({
          value: id,
          title: orgName,
          isLeaf: !hasChild,
          ...rest,
        }));
      }
      message.error('获取子组织失败');
      return [];
    },
  },
  reducers: {
    /* 更新状态（通用） */
    updateModel: (state, { payload }) => ({
      ...state,
      ...payload,
    }),
  },
};
